package com.meiyu.live.tv.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.meiyu.live.R;
import com.meiyu.live.popj.RoomDanmuMessage;
import com.meiyu.live.tv.utils.ChatRoomMessageParser;

/**
 * Created by zbl on 2016/9/1.
 */
public class DanmuView extends FrameLayout {

    private RoomDanmuMessage message;

    private ImageView iv_avatar;
    private TextView tv_text;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    public DanmuView(Context context) {
        super(context);
        init(context);
    }

    public DanmuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void setImageLoader(ImageLoader imageLoader, DisplayImageOptions options) {
        this.imageLoader = imageLoader;
        this.options = options;
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_danmu_item, this);
        tv_text = (TextView) findViewById(R.id.tv_text);
        iv_avatar = (ImageView) findViewById(R.id.iv_avatar);
    }

    public void setData(RoomDanmuMessage message) {
        this.message = message;
        if (message.getType() == ChatRoomMessageParser.TYPE_DANMU_MESSAGE) {
            tv_text.setText(message.getContent());
            imageLoader.displayImage(message.getAvatar(), iv_avatar, options);
        } else {
            //先构造SpannableString
            SpannableString spanString = new SpannableString("系统消息:恭喜" + message.getName() + "中了" + message.getValue() + "个钻石");
            //再构造一个改变字体颜色的Span
            ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
            //将这个Span应用于指定范围的字体
            spanString.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            //设置给EditText显示出来
            tv_text.setText(spanString);
            iv_avatar.setImageResource(R.drawable.icon_def_header);
        }
    }
}
